package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import com.itheima.health.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CheckItemServiceImpl
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/8/20 16:00
 * @Version V1.0
 */
// dubbo（整合声明式事务处理1：配置applicationContext-tx.xml对类代理，2：@Service(interfaceClass = CheckItemService.class)）
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;


    @Override
    public Member findMemberByTelephone(String telephone) {
        return memberDao.findMemberByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        // 注册会员（密码进行加密）
        if(member.getPassword()!=null && !member.getPassword().equals("")){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    // 使用注册时间查询，获取当前时间之前的会员数量统计
    // List<String> monthsList-->[2018-09, 2018-10, 2018-11, 2018-12, 2019-01, 2019-02, 2019-03, 2019-04, 2019-05, 2019-06, 2019-07, 2019-08]
    @Override
    public List<Integer> findMemberCountByRegTime(List<String> monthsList) {
        List<Integer> memeberCountList = new ArrayList<Integer>();
        if(monthsList!=null && monthsList.size()>0){
            for (String months : monthsList) {
                String regTime = months+"-31";
                Integer memeberCount = memberDao.findMemberCountByRegTime(regTime);
                memeberCountList.add(memeberCount);
            }
        }
        return memeberCountList;
    }

    // 统计性别人数占比（饼图）
    @Override
    public List<Map<String, Object>> findSexCountByOrder() {
        List<Map<String, Object>> list = memberDao.findSexCountByOrder();
        System.out.println(list);

        //查询所有会员的数量
        //Integer count = memberDao.getTotalMember();
        //查询男女会员数量
        //Integer countSex = memberDao.getTotalSexMember();
        for(Map<String, Object> stringObjectMap : list){
            //这个map里面又两个值，一个是key是sexname ,value是1 ，就是性别代码，另一个是key是value,value是4，就是性别人数
            String sexName = (String) stringObjectMap.get("sexName");
            if("1".equals(sexName)) {
                //这里一定要用name，因为在vue模板中定义的是name
                stringObjectMap.put("name","男");
            }else if ("2".equals(sexName)){
                stringObjectMap.put("name","女");
            }else if (sexName == null){
                stringObjectMap.put("name","该用户未填写性别");
            }
            stringObjectMap.remove("sexName");
        }
        return list;
    }

    //统计年龄人数占比（饼图）
    @Override
    public List<Map<String, Object>> findAgeCountByOrder() {

        return memberDao.findAgeCountByOrder();
    }

    @Override
    public int queryMemberByDate(String startDate, String endDate) {
        Map<Object,String> map = new HashMap<>();
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        return memberDao.queryMemberByDate(map);

    }
}
